package com.ziroom.zifamily.zfcensor.common.config.rocketmq;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import com.ziroom.zifamily.zfcensor.common.exception.RocketMQException;
/**
 * <p>TODO</p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年11月29日 16:48
 * @since 1.0
 */
@SpringBootConfiguration
public class RocketMQProducerConfiguration {

    @Value("${rocketmq.producer.groupName}")
    private String groupName;

    @Value("${rocketmq.producer.namesrvAddr}")
    private String namesrvAddr;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public DefaultMQProducer defaultMQProducer() {
        if (StringUtils.isEmpty(groupName)
                || StringUtils.isEmpty(namesrvAddr)) {
            throw new RocketMQException("MQ生产者groupName或namesrvAddr未配置");
        }
        DefaultMQProducer producer = new DefaultMQProducer(groupName);
        producer.setNamesrvAddr(namesrvAddr);
        return producer;
    }

}
